<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
</head>
<body>
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <div class="layui-row">
        <form class="layui-form" lay-filter="myform" id="myform">
          <input type="reset" id="btReset" class="layui-hide" >
          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">名称</label>
              <div class="layui-input-inline">
                <input type="text" name="name" id="name" autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-inline">
              <div class="layui-btn-group">
                <button type="button" class="layui-btn layui-btn-primary" data-type="query"><i class="layui-icon">&#xe615;</i>查询</button>
                <button type="button" class="layui-btn layui-btn-primary" data-type="resetBtn"><i class="layui-icon">&#xe669;</i>重置</button>
              </div>
            </div>
          </div>
        </form>
      </div>
      <div class="row">
        <table class="layui-hide"  id="main-table" lay-filter="main-table"></table>
      </div>
    </div>
  </div>
</div>
<iframe name="downloadifr" style="display: none"></iframe>
<a target="downloadifr" id="downloada" href="http://8.129.212.95:1799/zdtest/试题导入模版.docx">下载模板</a>
<button type="button" id="importWord" style="display: none">导入</button>
<button type="button" id="importWord2" style="display: none">导入</button>
<script th:inline="none"></script>
<script>
  let majorId = "",categoryId = "",lg = "",testPhase = "",gradeYear="",collegeId="",paperType="",semester="",studentType="",grade="",testType="",howlong='';
  layui.use(['table','form','upload'], function(){
    var table = layui.table,
            upload = layui.upload,
            form = layui.form;
    table.render({
      elem: '#main-table'
      ,height: (top.height-85)+'px'
      ,url: 'importRecord/getPageList'
      ,title: '题库列表'
      ,page: true
      ,limit:20
      ,toolbar: '#toolbarPrice'
      ,cols: [
        [
          {type: 'numbers'},
          {type: 'checkbox'}
          ,{field: 'code', title: "编号",width:150,templet:function(d){
            return "<a class='colum-link' href='javascript:void(0)' onclick='edit(\""+d.id+"\",\"" + d.code + "\" , this)'>" + d.code + "</a>";
          }}
          ,{field: 'createTime', title: "导入时间",width:170}
          ,{field: 'stateTxt',title: "记录状态",width:90}
          ,{field: 'testTypeTxt',title: "考试方式",width:90}
          ,{field: 'testPhaseTxt',title: "考试阶段",width:90}
          ,{field: 'paperTypeTxt',title: "A/B/C卷",width:90}
          ,{field: 'semesterTxt',title: "考试学期",width:90}
          ,{field: 'studentTypeTxt',title: "学生类型",width:90}
          ,{field: 'categoryName',title: "科目",width:150}
          ,{field: 'majorName',title: "专业",width:120}
          ,{field: 'collegeName',title: "学院/系",width:130}
          ,{field: 'lgTxt',title: "考试语言",width:90}
          ,{field: 'gradeName',title: "考生年级",width:100}
          ,{field: 'howlong',title: "考试时长",width:90}
          ,{field: 'gradeYearName',title: "考试学年",width:130}
          ,{field: 'createUserName',title: "操作人",width:110}
        ]
      ]
      ,done:function(){
        enableRowClick(table,form,'main-table',"main-table");
      }
    });
    //头工具栏事件
    table.on('toolbar(main-table)', function(obj){
      switch(obj.event){
        case 'importWordBtn':
          layer.open({
            id:"openwin",
            type: 2,
            title:'选择专业和科目',
            area:['1200px','680px'],
            content:  'rooter?url=ksxt/lib/import-win',
            btn:['导入','关闭'],
            yes:function(index,layero){
              var iframeWin = window[layero.find('iframe')[0]['name']];
              var obj = iframeWin.getIds();
              if(!obj.collegeId){
                layer.msg("请选择学院/系");
              }
              else if(!obj.major){
                layer.msg("请选择专业");
              }else if(!obj.category){
                layer.msg("请选择科目");
              }else if(!obj.testPhase){
                layer.msg("请选择考试阶段");
              }else{
                majorId = obj.major;
                categoryId = obj.category;
                lg = obj.lg;
                testPhase = obj.testPhase;
                gradeYear = obj.gradeYear;
                collegeId = obj.collegeId;
                howlong = obj.howlong;
                semester = obj.semester;
                grade = obj.grade;
                studentType = obj.studentType;
                paperType = obj.paperType;
                testType = obj.testType;
                $("#importWord").click();
                layer.close(index)

              }
              return false
            }
          });
          break;
        case 'importWordBtn2':
          var checkStatus = table.checkStatus('main-table');
          if(checkStatus.data.length==0){
            layer.msg("选择需要重新导入的记录");
            return false;
          }
          var id = checkStatus.data[0].id;
          var ir = getEntityInfo(id,'importRecord/getById')
           var state = ir.state;
          if(state==2){
            layer.msg("记录已确认，不可更改");
            return false;
          }
          $("#importWord2").click();
          break;
        case 'download':
          $("#downloada")[0].click();
          break;
        case 'help':
          layer.open({
            id:"openwin",
            type: 2,
            title:'导入试题帮助',
            area:['1000px','700px'],
            content:  'rooter?url=ksxt/lib/import-help',
            btn:['关闭']
          });
          break;
        case 'delete':
          var checkStatus = table.checkStatus('main-table');
          var data = checkStatus.data;
          if(data.length>0){
            var values = {};
            var ids=[];
            $.each(data,function(i,v){
              ids.push(v.id);
            })
            values['ids'] = ids.join(",");
            layer.confirm('确定要删除当前行么?', {icon: 3, title:'提示'}, function(index){
              axios.post("importRecord/delete",values).then(res=>{
                if (res.data.code == 1) {
                  layer.msg(res.data.msg);
                } else {
                  layer.close(index);
                  table.reload('main-table');
                  layer.msg("删除成功。");
                }
              })
            });
          }else{
            layer.msg("请选择操作数据!")
          }
          break;
        case 'confirm':
          var checkStatus = table.checkStatus('main-table');
          var data = checkStatus.data;
          if(data.length>0){
            var values = {};
            var ids=[];
            $.each(data,function(i,v){
              ids.push(v.id);
            })
            values['ids'] = ids.join(",");
            layer.confirm('确认后不可修改，继续操作吗?', {icon: 3, title:'提示'}, function(index){
              axios.post("importRecord/confirm",values).then(res=>{
                if (res.data.code == 1) {
                  layer.msg(res.data.msg);
                } else {
                  layer.close(index);
                  table.reload('main-table');
                  layer.msg("确认成功。");
                }
              })
            });
          }else{
            layer.msg("请选择操作数据!")
          }
          break;
        case 'edit':
          var checkStatus = table.checkStatus('main-table');
          var data = checkStatus.data;
          if(data.length===0){
            layer.msg("请选择导入记录!")
            return false;
          }
          layer.open({
            id:"openwin",
            type: 2,
            title:'修改试卷信息',
            area:['1000px','650px'],
            content:  'rooter?url=ksxt/lib/edit-import&id='+data[0].id,
            btn:['保存','关闭'],
            yes:function(index,layero){
              var dataForm = layer.getChildFrame('form', index);
              dataForm.find("#submitBtn").click();
              return false
            }
          });
          break;
      };
    });
    var url = 'libQuestion/importWord';
    upload.render({
      elem: '#importWord'
      ,url: url
      ,exts:"docx"
      ,before: function(obj){
        this.data.majorId = majorId
        this.data.categoryId = categoryId
        this.data.collegeId = collegeId;
        this.data.gradeYear = gradeYear;
        this.data.grade = grade;
        this.data.testPhase = testPhase;
        this.data.testType = testType;
        this.data.howlong = howlong;
        this.data.lg = lg;
        this.data.paperType = paperType;
        this.data.studentType = studentType;
        this.data.semester = semester;
        this.data.recordId = -1;
        this.data.state = 1;
      },done: function(res, index, upload) {
        layer.alert(res.msg)
        table.reload("main-table");
      },progress: function(n){

      },error: function(index, upload){

      }
    });
    upload.render({
      elem: '#importWord2'
      ,url: url
      ,exts:"docx"
      ,before: function(obj){
        var checkStatus = table.checkStatus('main-table');
        this.data.recordId = checkStatus.data[0].id;
        this.data.majorId = checkStatus.data[0].majorId;
        this.data.categoryId = checkStatus.data[0].categoryId;
        this.data.state = 1;
      },done: function(res, index, upload) {
        layer.alert(res.msg)
        table.reload("main-table");
      },progress: function(n){

      },error: function(index, upload){
        layer.alert("导入异常")
      }
    });
    var $ = layui.$, active = {
      query: function(){

        table.reload("main-table",{where:{name:$("#name").val()}})
      },
      resetBtn:function () {
        $("#btReset").click();

      }
    };
    $('.layui-form .layui-btn').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });

  });

  function edit(id){
    top.layer.open({
      id:"openwin12",
      type: 2,
      title:'查看导入结果',
      area:['1790px','780px'],
      content:  'rooter?url=ksxt/lib/import-result&id='+id,
      btn:['关闭']
    });
  }

</script>
<script type="text/html" id="toolbarPrice">
  <div class="layui-btn-group">
    <button class="layui-btn layui-btn-sm" lay-event="importWordBtn"><i class="layui-icon">&#xe6b2;</i>导入</button>
    <button class="layui-btn layui-btn-sm" lay-event="importWordBtn2"><i class="layui-icon">&#xe60a;</i>重新导入</button>
    <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="confirm"><i class="layui-icon">&#x1005;</i>确认</button>
<!--    <button class="layui-btn layui-btn-sm" lay-event="edit"><i class="layui-icon">&#xe607;</i>修改</button>-->
    <button class="layui-btn layui-btn-sm" lay-event="help"><i class="layui-icon">&#xe607;</i>帮助</button>
    <button class="layui-btn layui-btn-sm" lay-event="download"><i class="layui-icon">&#xe61e;</i>下载模板</button>
    <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</button>
  </div>
</script>
</body>
</html>